﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Model.SPECT;
using Mang24k99.Business.SPECT;
using System.Text.RegularExpressions;

namespace Mang24k99.Web.SpecialManage
{
    public partial class InvestSchool : System.Web.UI.Page
    {
        MKtSpectSubtype subtype = MKtSpectSubtype.SelectByPrimaryKey(3);
        protected void Page_Load(object sender, EventArgs e)
        {
            initTable();
            if (!IsPostBack)
            {
                Bindata();
            }
        }

        private void initTable()
        {
            Table myTable = new Table();
            for (int i = 1; i <= subtype.DisplayNum; i++)
            {
                //第一行
                TableRow tr1 = new TableRow();
                TableCell td11 = new TableCell();
                TableCell td12 = new TableCell();
                TableCell td13 = new TableCell();

                td11.Width = Unit.Percentage(20);
                td11.CssClass = "tb_txt1";
                Label labNO = new Label();
                labNO.Text = "第" + i + "则";
                td11.Controls.Add(labNO);

                td12.Width = Unit.Percentage(70);
                td12.CssClass = "tb_txt1";

                td13.Width = Unit.Percentage(10);
                td13.CssClass = "tb_txt1";
                tr1.Cells.Add(td11);
                tr1.Cells.Add(td12);
                tr1.Cells.Add(td13);
                myTable.Rows.Add(tr1);

                //第二行
                TableRow tr2 = new TableRow();
                TableCell td21 = new TableCell();
                TableCell td22 = new TableCell();
                TableCell td23 = new TableCell();

                td21.Width = Unit.Percentage(20);
                td21.CssClass = "tb_title1";
                td21.BorderWidth = 1;
                td21.BorderStyle = BorderStyle.Solid;
                Label labTitle = new Label();
                labTitle.Text = subtype.TitleDisplay;
                td21.Controls.Add(labTitle);

                td22.Width = Unit.Percentage(70);
                td22.CssClass = "tb_txt1";
                td22.BorderStyle = BorderStyle.Solid;
                td22.BorderWidth = 1;
                TextBox txtTitle = new TextBox();
                txtTitle.MaxLength = 20;
                txtTitle.ID = "txtTitle" + i;
                txtTitle.CssClass = "forms";
                txtTitle.Width = Unit.Percentage(100);
                td22.Controls.Add(txtTitle);

                td23.Width = Unit.Percentage(10);
                td23.CssClass = "tb_txt1";
                TextBox txtSort = new TextBox();
                txtSort.Attributes.Add("onkeypress", "verify(event)");
                txtSort.Attributes.Add("onPaste", "return false");
                txtSort.AutoCompleteType = AutoCompleteType.Disabled;
                txtSort.MaxLength = 3;
                txtSort.CssClass = "forms";
                txtSort.ID = "txtSort" + i;
                txtSort.Width = 40;
                td23.Controls.Add(txtSort);
                tr2.Cells.Add(td21);
                tr2.Cells.Add(td22);
                tr2.Cells.Add(td23);
                myTable.Rows.Add(tr2);

                //第三行
                TableRow tr3 = new TableRow();
                TableCell td31 = new TableCell();
                TableCell td32 = new TableCell();
                TableCell td33 = new TableCell();

                td31.Width = Unit.Percentage(20);
                td31.CssClass = "tb_title1";
                td31.BorderWidth = 1;
                td32.BorderStyle = BorderStyle.Solid;
                Label labAddr = new Label();
                labAddr.Text = subtype.AddresDsplay;
                td31.Controls.Add(labAddr);

                td32.Width = Unit.Percentage(70);
                td32.CssClass = "tb_txt1";
                td32.BorderStyle = BorderStyle.Solid;
                td32.BorderWidth = 1;
                TextBox txtAddr = new TextBox();
                txtAddr.ID = "txtAddr" + i;
                txtAddr.MaxLength = 60;
                txtAddr.CssClass = "forms";
                txtAddr.Width = Unit.Percentage(100);
                td32.Controls.Add(txtAddr);

                td33.Width = Unit.Percentage(10);
                td33.CssClass = "tb_txt1";
                tr3.Cells.Add(td31);
                tr3.Cells.Add(td32);
                tr3.Cells.Add(td33);
                myTable.Rows.Add(tr3);

            }
            this.Panel1.Controls.Add(myTable);

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (PageValidate() == false)
            {
                return;
            }

            try
            {
                string where = "detailitems.MKtSpectSubtype.SubjectType=3";
                MKtSpectDetailitems[] itemslist = (MKtSpectDetailitems[])BKtSpectDetailitems.SelectByWhere(where);
                //首先删除所有重要视角数据
                for (int i = 0; i < itemslist.Length; i++)
                {
                    BKtSpectDetailitems.Delete(itemslist[i]);
                }
                for (int i = 1; i <= subtype.DisplayNum; i++)
                {
                    TextBox txtTitle = this.Panel1.FindControl("txtTitle" + i) as TextBox;
                    TextBox txtAddr = this.Panel1.FindControl("txtAddr" + i) as TextBox;
                    TextBox txtSort = this.Panel1.FindControl("txtSort" + i) as TextBox;
                    if (txtTitle.Text.Trim() != "" && txtAddr.Text.Trim() != "" && txtSort.Text.Trim() != "")
                    {
                        MKtSpectDetailitems item = new MKtSpectDetailitems();
                        MKtSpectSubtype type = new MKtSpectSubtype();
                        type.SubjectType = 3;
                        item.SubjectNO = (int)BKtSpectDetailitems.GetMaxNO() + 1;
                        item.TITLE = txtTitle.Text;
                        item.ADDRESS = txtAddr.Text;
                        item.MKtSpectSubtype = type;
                        item.OrderNO = Convert.ToInt32(txtSort.Text);
                        item.IsValidate = 1;
                        item.InputTime = DateTime.Now;
                        BKtSpectDetailitems.Create(item);
                    }
                }

                string strMsg = "保存成功！";
                Common.MessageBox.Show(this.Page, strMsg);
                PageClear();
            }
            catch
            {
                string strMsg = "出现意外错误，保存失败！";
                Common.MessageBox.Show(this.Page, strMsg);
                return;
            }
            Bindata();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            PageClear();
            Bindata();
        }

        public bool PageValidate()
        {
            for (int i = 1; i <= subtype.DisplayNum; i++)
            {
                TextBox txtTitle = this.Panel1.FindControl("txtTitle" + i) as TextBox;
                TextBox txtAddr = this.Panel1.FindControl("txtAddr" + i) as TextBox;
                TextBox txtSort = this.Panel1.FindControl("txtSort" + i) as TextBox;
                //非全空
                if (!(txtTitle.Text.Trim() == "" && txtAddr.Text.Trim() == "" && txtSort.Text.Trim() == ""))
                {
                    //全部非空，验证排序号格式
                    if (txtTitle.Text.Trim() != "" && txtAddr.Text.Trim() != "" && txtSort.Text.Trim() != "")
                    {
                        string reg = @"^[1-9]\d*$";
                        Regex regex = new Regex(reg);
                        if (regex.IsMatch(txtSort.Text) == false)
                        {
                            string strMsg = "排序号格式必须为正整数，位置第" + i + "则";
                            Common.MessageBox.Show(this.Page, strMsg);
                            return false;
                        }

                        if (Convert.ToInt32(txtSort.Text.Trim()) == 0)
                        {
                            string strMsg = "排序号不能为0，位置第" + i + "则";
                            Common.MessageBox.Show(this.Page, strMsg);
                            return false;
                        }
                    }
                    //有一个或两个为空
                    else
                    {
                        string strMsg = "请输入完整，位置第" + i + "则";
                        Common.MessageBox.Show(this.Page, strMsg);
                        return false;
                    }
                }

            }
            return true;
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        public void Bindata()
        {
            string where = "detailitems.MKtSpectSubtype.SubjectType=3 order by detailitems.OrderNO,ID";
            MKtSpectDetailitems[] itemlist = (MKtSpectDetailitems[])BKtSpectDetailitems.SelectByWhere(where);
            for (int i = 0; i < itemlist.Length; i++)
            {
                int j = i + 1;
                TextBox txtTitle = this.Panel1.FindControl("txtTitle" + j) as TextBox;
                TextBox txtAddr = this.Panel1.FindControl("txtAddr" + j) as TextBox;
                TextBox txtSort = this.Panel1.FindControl("txtSort" + j) as TextBox;
                txtTitle.Text = itemlist[i].TITLE;
                txtAddr.Text = itemlist[i].ADDRESS;
                txtSort.Text = itemlist[i].OrderNO.ToString();
            }
        }

        public void PageClear()
        {
            for (int i = 1; i <= subtype.DisplayNum; i++)
            {
                TextBox txtTitle = this.Panel1.FindControl("txtTitle" + i) as TextBox;
                TextBox txtAddr = this.Panel1.FindControl("txtAddr" + i) as TextBox;
                TextBox txtSort = this.Panel1.FindControl("txtSort" + i) as TextBox;
                txtAddr.Text = "";
                txtSort.Text = "";
                txtTitle.Text = "";
            }
        }
    }
    }
